package com.danielagarza.app;

import java.util.List;

import com.danielagarza.app.modelos.Evento;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.os.Bundle;
import android.view.Menu;

public class Mapa extends android.support.v4.app.FragmentActivity {

	private GoogleMap mMap;
	private List<Evento> lst;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getIntent().getExtras();
		String lista = bundle.getString("puntos");
		lst = new Gson().fromJson(lista, new TypeToken<List<Evento>>() {}.getType());
		setContentView(R.layout.activity_mapa);
		setUpMapIfNeeded();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_home, menu);
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		setUpMapIfNeeded();
	}

	private void setUpMapIfNeeded() {
		if (mMap == null) {
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();
			if (mMap != null) {
				setUpMap();
			}
		}
	}

	private void setUpMap() {
		for (Evento evento : lst) {
			mMap.addMarker(new MarkerOptions().position(
					new LatLng(evento.latitud, evento.longitud)).title(evento.evento));
		}
	}

}
